# import asyncio
# import os
# import re
#
# from openai import AsyncOpenAI
# import requests
# import json
# import time
# import sys
# from config import DOUBAO_API_KEY, DOUBAO_API_URL
#
# def detect_image_type(base64_str: str) -> str:
#     if base64_str.startswith('iVBORw0KG'):
#         return "png"
#     elif base64_str.startswith('/9j/'):
#         return "jpeg"
#     elif base64_str.startswith('R0lGOD'):
#         return "gif"
#     elif base64_str.startswith('Qk03'):
#         return "bmp"
#     elif base64_str.startswith('SUkq'):
#         return "webp"
#     else:
#         return "unknown"
#
# async def ocr_generate_reply(base64, retry=3):
#         """生成回复"""
#         # 构建请求体
#
#         img_type = detect_image_type(base64)
#         img_type = img_type if img_type != "unknown" else "png"
#         client = AsyncOpenAI(api_key=DOUBAO_API_KEY, base_url=DOUBAO_API_URL)
#         for i in range(retry):
#             try:
#                 completion = await client.chat.completions.create(
#                     # model="doubao-seed-1.6-250615",
#                     model="gpt-4o",
#                     messages=[
#                         {
#                             "role": "user",
#                             "content": [
#                                 {
#                                     "type": "image_url",
#                                     "image_url": {
#                                         "url": f"data:image/{img_type};base64,{base64}",
#                                     }
#                                 },
#                                 {
#                                     "type": "text",
#                                     "text": "请识别图片信息，直接将图片的信息用文本输出（只做识别），输出格式：{\"类型\":\"图片\",\"图片OCR识别后的文本\":\"内容\"}"
#                                 }
#                             ]
#                         }
#                     ]
#                 )
#                 reply = completion.choices[0].message.content
#                 return reply
#             except Exception as e:
#                 print(f"Error: {e}")
#                 await asyncio.sleep(1)
#
